# Copyright (c) Alessandro Pilotti 2009
# http://www.codeplex.com/PowerQuery
# info@pilotti.it

param( 
    [string] $action = "register" 
)

function GetScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}

function CheckInstallUtilAlias()
{
  if((get-alias | Where-Object {$_.Name -eq "installutil"}) -eq $null)
  {
    Set-Alias -scope Global installutil (Join-Path ([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) "installutil.exe")
  }
}

function RegisterPowerQuery()
{
  CheckInstallUtilAlias

  installutil PowerGAC.dll

  Add-PSSnapIn AlexPilotti.PowerGAC -ErrorAction SilentlyContinue

  Register-Assembly DynamicQuery.dll
  Register-Assembly L2SQLDataModelGenerator.dll
  Register-Assembly PowerQuery.dll

  installutil PowerQuery.dll
}


function UnregisterPowerQuery()
{
  CheckInstallUtilAlias
  
  Add-PSSnapIn AlexPilotti.PowerGAC -ErrorAction SilentlyContinue

  installutil /u PowerQuery.dll

  Unregister-Assembly DynamicQuery.dll
  Unregister-Assembly L2SQLDataModelGenerator.dll
  Unregister-Assembly PowerQuery.dll

  installutil /u PowerGAC.dll
}

cd $(GetScriptDirectory)

switch($action)
{
	"register" { RegisterPowerQuery; break }
	"unregister" { UnregisterPowerQuery; break }
}